package com.tarena.demo.luban.all.main.controller;


import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.tarena.demo.luban.all.main.alarm.MessageAlarm;
import com.tarena.demo.luban.all.main.service.OrderService;
import com.tarena.demo.luban.commons.exception.BusinessDemoException;
import com.tarena.demo.luban.commons.restful.JsonResult;
import com.tarena.demo.luban.commons.restful.ResponseCode;
import com.tarena.demo.luban.protocol.order.param.OrderAddParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/order")
@Api(tags = "订单")
public class OrderController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired(required = false)
    private MessageAlarm messageAlarm;
    @PostMapping("/add")
    @ApiOperation("新增订单的功能")
    @SentinelResource(
            value="addOrder",
            blockHandler = "addOrderBlock",
            fallback = "addOrderError")
    public JsonResult addOrder(OrderAddParam orderAddParam){
        //组织一个消息 发送到队列主题 luban-order-topic
        //消息内的数据 满足原则: 精简(携带数据量小) 准确(满足业务需求)
        Message<OrderAddParam> message =
                MessageBuilder.withPayload(orderAddParam).build();
       /* SendResult sendResult = rocketMQTemplate.syncSend("aaa", message);
        SendStatus sendStatus = sendResult.getSendStatus();
*/
        rocketMQTemplate.asyncSend("luban-order-topic", message,
                new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        //messageAlarm.sendSuccess(new Object());
                        System.out.println("成功");
                    }
                    @Override
                    public void onException(Throwable throwable) {
                        //messageAlarm.sendError(new Object());
                        System.out.println("失败");
                    }
                });
        return JsonResult.ok("订单已提交");
    }
    //sentinel流控拦截 降级
    public JsonResult addOrderBlock(OrderAddParam orderAddParam,BlockException e){
        return JsonResult.ok("新增订单失败!");
    }
    //系统 业务异常降级
    public JsonResult addOrderError(OrderAddParam orderAddParam,Throwable e){
        return JsonResult.ok("新增订单失败!");
    }


}
